<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取时间" prop="collectionTime">
        <el-date-picker clearable
                        v-model="queryParams.collectionTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择领取时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="使用状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择使用状态" clearable>
          <el-option
            v-for="dict in dict.type.shop_coupon_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cart:userCoupon:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cart:userCoupon:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cart:userCoupon:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cart:userCoupon:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userCouponList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="商家编号" align="center" prop="shopsId"/>
      <el-table-column label="用户编号" align="center" prop="userId"/>
      <el-table-column label="优惠券编号" align="center" prop="couponId"/>
      <el-table-column label="领取时间" align="center" prop="collectionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.collectionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_coupon_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cart:userCoupon:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cart:userCoupon:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户优惠券对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-form-item label="商家编号" prop="shopsId">
          <el-input v-model="form.shopsId" placeholder="请输入或选择商家编号" style="width: 50%"/>
<!--          <el-button type="text" @click="table = true" style="margin-left: 10px">点击选择商家</el-button>-->
        </el-form-item>

        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户编号" style="width: 50%"/>
          <el-button type="text" @click="userTable = true" style="margin-left: 10px">点击选择用户</el-button>
        </el-form-item>

        <el-form-item label="优惠券编号" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入优惠券编号" style="width: 50%"/>
          <el-button type="text" @click="couponTable = true" style="margin-left: 10px">点击选择优惠券</el-button>
        </el-form-item>

        <el-form-item label="使用状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择使用状态">
            <el-option
              v-for="dict in dict.type.shop_coupon_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--选择商家抽屉弹框begin-->
    <el-drawer
      title="选择商家"
      :visible.sync="table"
      direction="rtl"
      size="50%">
      <!--搜索商家条件表单-->
      <el-form :model="queryShopParams" ref="queryShopForm" size="small" :inline="true"
               v-show="showSearch" label-width="68px" style="margin: 10px">
        <el-form-item label="">
          <el-input
            v-model="queryShopParams.shopsId"
            placeholder="请输入商家编号"
            clearable
            @keyup.enter.native="handleShopQuery"
          />
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="queryShopParams.shopName"
            placeholder="请商家名称"
            clearable
            @keyup.enter.native="handleShopQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleShopQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetShopQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!--商家列表-->
      <el-table :data="gridData" style="margin: 10px">
        <el-table-column property="shopsId" label="商家编号" align="center" width="80px"></el-table-column>
        <el-table-column label="logo" align="center">
          <template v-slot="slot">
            <img v-if="slot.row.shopLogo" :src="slot.row.shopLogo" alt="" style="height: 30px;width: 30px">
          </template>
        </el-table-column>
        <el-table-column property="shopName" label="商家名称" align="center"></el-table-column>
        <el-table-column property="shopUrl" label="地址" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="slot">
            <el-button type="info" size="mini" @click="setShopId(slot.row.shopsId)">添加</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>
    <!--选择商家抽屉弹框end-->

    <!--选择用户抽屉弹框begin-->
    <el-drawer
      title="选择用户"
      :visible.sync="userTable"
      direction="rtl"
      size="50%">
      <!--搜索用户条件表单-->
      <el-form :model="queryUserParams" ref="queryUserForm" size="small" :inline="true"
               v-show="showSearch" label-width="68px" style="margin: 10px">
        <el-form-item label="">
          <el-input
            v-model="queryUserParams.userId"
            placeholder="请输入用户编号"
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item label="">
          <el-input
            v-model="queryUserParams.userName"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
<!--        <el-form-item label="">-->
<!--          <el-input-->
<!--            v-model="queryUserParams.nickName"-->
<!--            placeholder="请输入用户昵称"-->
<!--            clearable-->
<!--            @keyup.enter.native="handleUserQuery"-->
<!--          />-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUserQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetUserQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!--用户列表-->
      <el-table :data="gridUserData" style="margin: 10px">
        <el-table-column property="userId" label="用户编号" align="center" width="80px"></el-table-column>
        <el-table-column property="userName" label="用户名称" align="center"></el-table-column>
        <el-table-column property="nickName" label="用户昵称" align="center"></el-table-column>
        <el-table-column property="deptId" label="部门" align="center"></el-table-column>
        <el-table-column property="phonenumber" label="手机号码" align="center"></el-table-column>
        <el-table-column property="status" label="状态" align="center"></el-table-column>
        <el-table-column property="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="slot">
            <el-button type="info" size="mini" @click="setUserId(slot.row.userId)">添加</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>
    <!--选择用户抽屉弹框end-->

    <!--选择优惠券抽屉弹框begin-->
    <el-drawer
      title="选择优惠券"
      :visible.sync="couponTable"
      direction="rtl"
      size="50%">
      <!--搜索优惠券条件表单-->
      <el-form :model="queryCouponParams" ref="queryCouponForm" size="small" :inline="true"
               v-show="showSearch" label-width="68px" style="margin: 10px">
        <el-form-item label="编号">
          <el-input
            v-model="queryCouponParams.couponId"
            placeholder="请输入优惠券编号"
            clearable
            @keyup.enter.native="handleCouponQuery"
          />
        </el-form-item>
        <el-form-item label="名称">
          <el-input
            v-model="queryCouponParams.name"
            placeholder="请输入优惠券名称"
            clearable
            @keyup.enter.native="handleCouponQuery"
          />
        </el-form-item>
        <el-form-item label="优惠券类型" prop="type">
          <el-select v-model="queryCouponParams.type" placeholder="请选择优惠券类型" clearable>
            <el-option
              v-for="dict in dict.type.shop_coupon_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleCouponQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetCouponQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!--优惠券列表-->
      <el-table :data="gridCouponData" style="margin: 10px">
        <el-table-column property="couponId" label="编号" align="center" width="80px"></el-table-column>
        <el-table-column property="name" label="优惠券名称" align="center" ></el-table-column>
        <el-table-column label="优惠券类型" align="center" prop="type">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.shop_coupon_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column property="fullAmount" label="满额" align="center"></el-table-column>
        <el-table-column property="face" label="面额" align="center"></el-table-column>
        <el-table-column property="discount" label="折扣" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="slot">
            <el-button type="info" size="mini" @click="setCouponId(slot.row.couponId)">添加</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>
    <!--选择优惠券抽屉弹框end-->
  </div>
</template>

<script>
import {
  listUserCoupon,
  getUserCoupon,
  delUserCoupon,
  addUserCoupon,
  updateUserCoupon,
  listShop, listUser, listCoupon
} from "@/api/cart/userCoupon";

export default {
  name: "UserCoupon",
  components: {},
  dicts: ['shop_coupon_status','shop_coupon_type', 'shop_coupon_status1'],
  data() {
    return {
      //商家抽屉是否打开
      table: false,
      //用户抽屉是否打开
      userTable:false,
      //优惠券抽屉是否打开
      couponTable:false,
      dialog: false,
      //商家表单数据
      gridData: [],
      //用户表单数据
      gridUserData: [],
      //优惠券表单数据
      gridCouponData:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户优惠券表格数据
      userCouponList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        collectionTime: null,
        status: null
      },
      //商家搜索条件
      queryShopParams: {
        shopsId: null,
        shopName: null,
      },
      //用户搜索条件
      queryUserParams: {
        userId: null,
        userName: null,
      },
      //优惠券搜索条件
      queryCouponParams:{
        couponId:null,
        name:null,
        status:null,
      },
      // 表单参数
      form: {
        shopsId: null,
        userId: null,
        couponId: null
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    // this.getShopList();
    this.getUserList();
    this.getCouponList();
  },
  methods: {
    setShopId(id) {
      this.table = false
      this.form.shopsId = id;
    },
    setUserId(id) {
      this.userTable = false
      this.form.userId = id;
    },
    setCouponId(id) {
      this.couponTable = false
      this.form.couponId = id;
    },
    /** 查询用户优惠券列表 */
    getList() {
      this.loading = true;
      listUserCoupon(this.queryParams).then(response => {
        this.userCouponList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询商家列表 */
    getShopList() {
      listShop(this.queryShopParams).then(response => {
        this.gridData = response.rows;
      });
    },
    /** 查询用户列表 */
    getUserList() {
      listUser(this.queryUserParams).then(response => {
        this.gridUserData = response.rows;
      });
    },
    /** 查询优惠券列表 */
    getCouponList() {
      listCoupon(this.queryCouponParams).then(response => {
        this.gridCouponData = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shopsId: null,
        userId: null,
        couponId: null,
        collectionTime: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 商家搜索按钮操作 */
    handleShopQuery() {
      this.queryParams.pageNum = 1;
      this.getShopList();
    },
    /** 商家搜索重置按钮操作 */
    resetShopQuery() {
      this.queryShopParams.shopsId = null;
      this.queryShopParams.shopName = null;
      this.handleShopQuery();
    },
    /** 用户搜索按钮操作 */
    handleUserQuery() {
      this.queryParams.pageNum = 1;
      this.getUserList();
    },
    /** 用户搜索重置按钮操作 */
    resetUserQuery() {
      this.queryUserParams.userId = null;
      this.queryUserParams.userName = null;
      this.handleUserQuery();
    },
    /** 优惠券搜索按钮操作 */
    handleCouponQuery() {
      this.queryParams.pageNum = 1;
      this.getCouponList();
    },
    /** 优惠券搜索重置按钮操作 */
    resetCouponQuery() {
      this.resetForm("queryCouponForm");
      this.handleCouponQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户优惠券";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserCoupon(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户优惠券";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUserCoupon(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserCoupon(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户优惠券编号为"' + ids + '"的数据项？').then(function () {
        return delUserCoupon(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/userCoupon/export', {
        ...this.queryParams
      }, `userCoupon_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
